<?php
		session_start();		
	/*###### START INCLUDES ######*/
		include('db_connect.php');
		include('forms.php');
		include('cookie.php');
		include('login_functions.php');
		include('register_functions.php');
		include('content_functions.php');
		include('entry_functions.php');
		include('search_functions.php');
	/*###### ENDE INCLUDES ######*/	
	
	$status = '0';
	$changestatus = '0';
	
	if($sucsess = connect()){
			if(isset($_POST['change']) && $_POST['change']=="Änderungen speichern") {
				$changestatus = updateUser();
				$change = '1';
				if($changestatus == '1'){
					deleteCookie();
					session_destroy();
				}
				else{
					$status = '1';
				}
			}
			elseif (isset($_POST['login']) && $_POST['login']=="Login") {
				$status = login();
				$email = $_POST['email'];	
			}elseif (isset($_COOKIE["email"]) && isset($_COOKIE["pass"])) {	
				$email = $_COOKIE["email"];
				$pass = $_COOKIE["pass"];
				$status = loginSession($email, $pass);
			}elseif (isset($_SESSION["email"]) && isset($_SESSION["pass"])){
				$email = $_SESSION["email"];
				$pass = $_SESSION["pass"];
				$status = loginSession($email, $pass);
			}
		}
	if(isset($email)){
		$_SESSION['userID'] = getUserID($email);
	}
	
	$_SESSION['status'] = $status;
	
?>
<?php
$content = array();
$content['start'] = 'Herzlich Willkommen';
$content['entry'] = 'Mitfahrgelegenheit eintragen';
$content['result'] = 'Ihr Suchergebnis';
$content['reg'] = 'Registrierung';
$content['edit'] = 'Daten bearbeiten';
$content['lift'] = 'Mitfahren';
$content['faq'] = 'FAQ';
$content['contact'] = 'Kontakt';
$content['impressum'] = 'Impressum';
$content['logout'] = 'Logout';


if (!isset($_GET['content'], $content[$_GET['content']])) {
    $title = $content['start'];
    $_GET['content'] = 'start';
}
elseif(isset($content[$_GET['content']])){
	$title = $content[$_GET['content']];
	}
else {die;}
?>
<!DOCTYPE html>
<html>
<head>
	<title>fahrmit.de -  
	
	<?php pagetitle($_GET['content']); ?>
	
	</title>
	
	<meta property="og:title" content="fahrmit.de - <?php pagetitle($_GET['content']); ?>" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="css/reset.css">
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.23.custom.css" rel="stylesheet" />
	<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.23.custom.min.js"></script>
	<script type="text/javascript">
		$(function() {
		$( ".datepicker" ).datepicker({ minDate: 0, dateFormat: "dd-mm-yy", monthNames: ["Januar","Februar","März","April","May","Juni","Juli","August","September","Oktober","November","Dezember"], dayNamesMin: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"], firstDay: 1} );
	});
	
	</script>
</head>
<body>
	<header id="header">
		<div id="topMenuBackground">
			<nav id="topMenu">
				<ul>
					<li><a href="index.php?content=faq">FAQ</a></li>
					<li><a href="index.php?content=contact">Kontakt</a></li>
					<li><a href="index.php?content=impressum">Impressum</a></li>
				</ul>
			</nav>
		</div>
		<nav id="mainMenu">
			<ul>
				<li><a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">Startseite</a></li>
				<li><a href="index.php?content=entry">Eintragen</a></li>
			</ul>
			<section id="loginBox">
				<?php 
					if($status == '0'){
					?><div id="login_status"><?php
					printLoginForm();
					?></div><a href="index.php?content=reg&reg=false">Jetzt regestrieren</a><?php
					}
					elseif($status == '1'){
						if(!isset($email)){
							$email = $_POST["email"];
						}
						$abfrage = "SELECT ID, email, name, vorname FROM user WHERE email='".mysql_real_escape_string($email)."' LIMIT 1";
						$ergebnis = mysql_query($abfrage);
						$row = mysql_fetch_object($ergebnis);
						$id = $row->ID;
						$name = $row->name;
						$vorname = $row->vorname;
						
						echo '<div id="login_status">Hallo '.$vorname.' '.$name.'</div>';
						echo "\n";
						?>
						<a href="index.php?content=edit">Einstellungen</a>
						&nbsp;|&nbsp;
						<?php logOut();?><?php
					}
					elseif($status == '2'){
						?><div id="login_status"><?php
						printLoginForm();
						?></div>
						<span>Benutzername und/oder Passwort waren falsch</span>&nbsp;|&nbsp;
						<a href="index.php?content=reg&reg=false">Jetzt regestrieren</a><?php
					}elseif($status == '3'){
						?><div id="login_status"><?php
						printLoginForm();
						?></div>
						<span>Bitte erneut anmelden</span><?php
					}

					
					
				?>
			</section>
		</nav>
		<section id="banner">
			<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"><img src="img/logo.png" id="logo" alt="Mitfahrgelegenheit"/></a>
		</section>
	</header>
	<section id="content">
		<h1><?php echo $title; ?></h1>
		<section id="topContent">
			<?php
							
				
				mainContent($_GET['content'],$changestatus);
			
			?>
		</section>
		<section id="middleContent">
			<section id="middleLeftContent">
				<h2>Suchen</h2>
				<?php printSearchForm();
				 ?>
			</section>
			<section id="middleRightContent">
				<h2>Aktuelle Mitfahrgelegenheiten</h2>
				<?php lastRides(); ?>
			</section>
			<div class="clearer"></div>
		</section>
		<section id="bottomContent">
		</section>
	</section>
	<footer id="footer">
	</footer>
</body>
</html>